//
//  HFCustomLayoutTow.m
//  ThridPackage
//
//  Created by 韩艳锋 on 2019/7/29.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "HFCustomLayoutTow.h"
#import "CMDefine.h"

@interface HFCustomLayoutTow ()

//@property (assign, nonatomic) CGFloat collectionItemHeigh;
//@property (assign, nonatomic) CGFloat spaceOne;
//@property (assign, nonatomic) CGFloat spaceTow;
@property (strong, nonatomic) NSMutableArray * attributesArr;

@property (assign, nonatomic) CGFloat collectionContentHeigh;

@end

@implementation HFCustomLayoutTow

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.herSpace = 10;
        self.verSpace = 10;
        self.numberOfCol = 2;
        self.liftRightSpace = 10;
        self.topSpace = 15;
//        self.herSpace = 0;
//        self.verSpace = 0;
//        self.numberOfCol = 2;
//        self.liftRightSpace = 0;
//        self.topSpace = 0;
        self.collectionContentWidth = SCREEN_WIDTH;
        self.attributesArr = [NSMutableArray array];
    }
    return self;
}

- (void)prepareLayout {
    [super prepareLayout];
    self.collectionContentHeigh = 0;
    if (!self.getHeigh) {
        return;
    }
    NSInteger itemNum = [self.collectionView numberOfItemsInSection:0];
    self.attributesArr = [NSMutableArray arrayWithCapacity:itemNum];
    CGFloat width = (self.collectionContentWidth - self.liftRightSpace * 2 - self.herSpace) / self.numberOfCol;
    CGPoint origins[self.numberOfCol];
    for (int i = 0; i != self.numberOfCol; i++) {
        origins[i] = CGPointMake(self.liftRightSpace + (width + self.herSpace) * i, self.topSpace);
    }
    CGPoint currentOrigin;
    int index = 0;
    for (int i = 0; i != itemNum; i++) {
        CGFloat maxY = 0xfffffffffffffff;
        for (int j = 0; j != self.numberOfCol; j++) {
            if (origins[j].y < maxY) {
                maxY = origins[j].y;
                index = j;
            }
        }
        currentOrigin = origins[index];

        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes * attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        [self.attributesArr addObject:attribute];
        attribute.frame = CGRectMake(currentOrigin.x, currentOrigin.y, width, self.getHeigh(indexPath, width));
        origins[index] = CGPointMake(origins[index].x, origins[index].y + attribute.frame.size.height + self.verSpace);
        CGFloat max = CGRectGetMaxY(attribute.frame);
        if (max > self.collectionContentHeigh) {
            self.collectionContentHeigh = max;
        }
    }
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    if (CGSizeEqualToSize(newBounds.size, [self collectionViewContentSize])) {
        return YES;
    } else {
        return NO;
    }
}
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    //    NSInteger staIndex = 0;
    //    NSInteger endIndex = self.attributesArr.count - 1;
    //    NSInteger currentIndex = (staIndex + endIndex)/2;
    //    while (staIndex<endIndex) {
    //        UICollectionViewLayoutAttributes * arr = self.attributesArr[currentIndex];
    //    }
    //
    //    if (staIndex>=endIndex || currentIndex == -1) {
    //        return nil;
    //    }
    //
    return self.attributesArr;
//    NSMutableArray * bacArr =  [NSMutableArray arrayWithCapacity:self.attributesArr.count];
//    for (NSInteger i = 0; i != self.attributesArr.count; i++) {
//        UICollectionViewLayoutAttributes * att = self.attributesArr[i];
//        if (CGRectIntersectsRect(att.frame, rect)) {
//            [bacArr addObject:att];
//        } else {
//            break;
//        }
//    }
//    return bacArr;
}

- (CGSize)collectionViewContentSize {
    return CGSizeMake(self.collectionContentWidth, self.collectionContentHeigh);
}
@end
